#' Testing script: Derive reflectance and temperature from one Landsat tile and apply the Fmask
#' @name LReTe-package
#' @aliases LReTe
#' @docType package
#' @author Joseph Henry \email{joseph.henry@@sydney.edu.au}, Willem Vervoort \email{willem.vervoort@@sydney.edu.au}, Dennis Duro \email{dennis.duro@@glel.carleton.ca} and Mark Adams.

########################### User defined variables ###################################
################################### START ############################################
	# Paths
		DD.path <- "W:/"
		JH.path.1 <- "C:/Users/Uni/Desktop/NANO/USYD/desktop"
		WV.path <- ""
		BASE_PATH <- JH.path.1
	# Bounding box to subset images to
		JH.BBOX <- c(650000,660000,-3948000,-3936000) #c(650000,678000,-3960000,-3936000) # UTM
		DD.BBOX <- ""
		WV.BBOX <- ""
		BBOX.VAL <- JH.BBOX
	# Decision
		CREATE_DIR_DECISION <- "yes" # #"no" this will create the directory for you to dump the look up tables and landsat data
		OVERWRITE_DIR_DECISION <- "no" # "yes"  # Prevent/allow you overwriting an already created directory 	# TO DO: Remove OVERWRITE_DIR_DECISION. It doesn't do anything. If the file exists, it is not overwritten by ?dir.create.
		run.prob.over.water <- "no"# choice,  "yes" or  "no", to run Fmask over water. JH didn't use this because no large water bodies in catchment. If "yes". 120423_Fmask.r may need to be checked.
###################################### END #############################################
########################################################################################	

### Preliminary work
	## Packages (dependicies of the package)
		if (!require(stringr)){
				install.packages('stringr')
				require(stringr)
		} 
		if (!require(raster)){
				install.packages('raster')
				require(raster)
		}
		if (!require(rgdal)){
				install.packages('rgdal')
				require(rgdal)
		}
		if (!require(RSAGA)){
				install.packages('RSAGA')
				require(RSAGA)
		}  
		if (!require(igraph0)){
				install.packages('igraph0')
				require(igraph0)
		}
		
	## Pathways that hang off the one you decided on (BASE_PATH)	
		DATA_DIR <- paste(BASE_PATH, "Fmask_test", sep = "/") # Your fmask test folder, which contains the 'Image','Output' and 'LUT' folders
			## Folders where you have to manually put stuff
				DATA_DIR_IMAGE <- paste (DATA_DIR, "Image", sep = "/")# Must contain all of the Landsat data once its unzipped using from the tar/gz .etc
				DATA_DIR_IMAGE_SUB <- paste (DATA_DIR, "Image_sub", sep = "/") # Place where raw clips will go. Fmask is performed on these.
				DATA_DIR_LUT <- paste (DATA_DIR, "LUT", sep = "/") # Must contain: 101201_ETM_Solar_Spectra_Irradiances.csv , 101201_Earth-Sun distance.csv 	, 101201_LTM_Spectral_Radiance_Range.csv
			## Folders that are updated after running the scripts
				DATA_DIR_TR <- paste (DATA_DIR, "Output/TR", sep = "/") # Temperature and reflectance
				DATA_DIR_FMASK <- paste (DATA_DIR, "Output/Fmask", sep = "/") # Fmask
	
	## Create the directory?
		if(CREATE_DIR_DECISION == "yes" ) {
			if(OVERWRITE_DIR_DECISION == "no") {
				dir.create(DATA_DIR)
				dir.create(DATA_DIR_IMAGE)
				dir.create(DATA_DIR_IMAGE_SUB)
				dir.create(DATA_DIR_LUT)
				dir.create(DATA_DIR_TR, recursive = TRUE)
				dir.create(DATA_DIR_FMASK, recursive = TRUE)
			}
		}
	
### Load functions
	# Source 120423_other_functions.R and 20423_Fmask_alpha.R

### Set-up before running functions
	## Set data for functions
		# Variables
			L7.COEFFICIENT.ZHU <- 10000 
			L7.FILLVALUE <- 255
		# Pathway for images
			DATA_FN_RAW <- dir (DATA_DIR_IMAGE)
			DATA_L7_RAW <- list.files(DATA_DIR_IMAGE, recursive = TRUE)
			DATA_PATH_RAW_FULL <- paste (DATA_DIR_IMAGE, grep(".gz",DATA_L7_RAW[grep(".TIF|.tif",DATA_L7_RAW)], invert = TRUE, value = TRUE), sep = "/")
			DATA_PATH_RAW <- (gsub("//","/",gsub(strsplit(DATA_L7_RAW[1],"/")[[1]][1],"",gsub( DATA_DIR_IMAGE, DATA_DIR_IMAGE_SUB,DATA_PATH_RAW_FULL))))
			DATA_PATH_RAW_EXCL <- L7.BANDNAMES.12345617 <- DATA_PATH_RAW[ - (grep ("B62|B80", DATA_PATH_RAW))] # Exclude one of the Band 6 images and Band 8 image
		# For each full image, subset and save it
			if(length(dir (DATA_DIR_IMAGE_SUB))==0){
				for(i in 1:length (DATA_PATH_RAW)){
					L7CLIP(DATA_PATH_RAW_FULL[i],DATA_PATH_RAW[i],BBOX.VAL)
				print (i)
			}
		}
		# Pathway and loading of metadata
			# MRT_FN <- dir (DATA_DIR_METADATA) # Metadata files
			MRT_PATH_RAW <- paste (DATA_DIR_IMAGE, grep(".gz",DATA_L7_RAW[grep("MTL",DATA_L7_RAW)], invert = TRUE, value = TRUE), sep = "/")
			#paste (DATA_DIR_METADATA, MRT_FN, sep = "/") # Full name and path of metadata
			MTL_all <- scan (MRT_PATH_RAW, what = "character", sep = "\t")
		# Output images
			DATA_TR_OUT.RAW <- OUTPUT.NAME <- gsub ("/Image/", "/Output/TR/", DATA_PATH_RAW_EXCL) # Output temperature and radiation images to be made
			# Modify for its own folder 
			if(length(grep(".tar",DATA_TR_OUT.RAW[1]))!=0){
				DATA_TR_OUT <- str_replace_all(DATA_TR_OUT.RAW,"(.*/)(LE7.*)([.]tar/)(.*)","\\1\\2/\\4")
				dir.create(unique(str_replace(DATA_TR_OUT.RAW,"(.*/)(LE7.*)([.]tar/)(.*)","\\1\\2")))
			} else {
				DATA_TR_OUT <- DATA_TR_OUT.RAW
			}
		L7.FN.PASSNAME.CONSTANTBITEXTRACT <- unique (str_extract (DATA_FN_RAW[grep(".TIF|.tif",DATA_FN_RAW)], "[0-9]{11,11}"))
		# Look up table pathways and load
			L7.FN.LUT.SPECTRALRADIANCERANGE <- paste (DATA_DIR_LUT, "101201_LTM_Spectral_Radiance_Range.csv", sep = "/") # Spectral radiance range
			L7.FN.LUT.EARTHSUNDISTANCE <- paste (DATA_DIR_LUT, "101201_Earth-Sun distance.csv", sep = "/") # Earth-Sun distance
			L7.FN.LUT.SOLARSPECTRAIRRADIANCE <- paste (DATA_DIR_LUT, "101201_ETM_Solar_Spectra_Irradiances.csv", sep = "/") # Solar sprecta irradiance
			L7.READ.LUT.SPECTRALRADIANCERANGE <- read.csv (L7.FN.LUT.SPECTRALRADIANCERANGE, stringsAsFactors = FALSE)
			L7.READ.LUT.EARTHSUNDISTANCE <- read.csv (L7.FN.LUT.EARTHSUNDISTANCE, stringsAsFactors = FALSE)
			L7.READ.LUT.SOLARSPECTRAIRRADIANCE <- read.csv (L7.FN.LUT.SOLARSPECTRAIRRADIANCE, stringsAsFactors = FALSE)
		# Make output folder (if necessary)
			
		
### Run the functions		
	# Get temperature and reflectance from radiance
		print ("+++++++++++++++++++++++++++++++++++LReTe start +++++++++++++++++++++++++++++++++++")
			print(Sys.time())
				RUN.PREP.FUNC <- LReTe (DATA_PATH_RAW_EXCL, DATA_TR_OUT, MTL_all, TRUE) # Create reflectance and at sensor temperature
			print ("+++++++++++++++++++++++++++++++++++LReTe finish +++++++++++++++++++++++++++++++++++")
		print(Sys.time())
		# Most memroy use occurs, I think, with ?writeRaster. ?writeValues may be more efficient? 
		# Function is run and some required values are saved to RUN.PREP.FUNC 

## Fmask
	# Set-up
		INPUT_BANDS <- dir (full.names = TRUE, DATA_DIR_TR) # file paths of reflectance and temperature data (bands 1 to 7) (character)
		INPUT_BANDS_DN <- DATA_PATH_RAW[1:3] # file paths of digital number data (bands 1 to 3) (character)
		OUTPUT <- DATA_DIR_FMASK # file path of output file (character).
		L7.SUN.AZIMUTH.TAN.RADIANS <- RUN.PREP.FUNC [1] # sun azimuth in radians derived from metadata file that comes with tile (numeric)
		L7.SUN.AZIMUTH <- RUN.PREP.FUNC [2] # sun azimuth in degrees derived from metadata file that comes with tile (numeric)
		L7.SUN.ELEVATION.TAN.RADIANS <- RUN.PREP.FUNC[3] #  sun elevation derived from metadata file that comes with tile (numeric)
	# Check for any big files and get rid of them 
		.ls.objects ()
	# May take a while
		FmaskForR(DATA_TR_OUT, DATA_PATH_RAW_EXCL, "no", L7.SUN.AZIMUTH.TAN.RADIANS, L7.SUN.AZIMUTH, L7.SUN.ELEVATION.TAN.RADIANS,"C:/Users/Uni/Desktop/fmask.tif")